package com.kzy.demoadmin.web.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageInfo;
import com.kzy.demoadmin.web.config.AutoLog;
import com.kzy.demoadmin.web.entity.*;
import com.kzy.demoadmin.web.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("book")
public class BookController {

    @Autowired
    private BookService bookService;

    /**
     * 保存书籍（新增、修改）
     *
     * @param book
     * @return
     */
    @PostMapping("/save")
    public Result add(@RequestBody Book book) {
        if (book.getId() == null) {
            bookService.add(book);
        } else {
            bookService.edit(book);
        }
        return Result.success();
    }


    /**
     * 分页查询+模糊查询
     *
     * @param query
     * @return
     */
    @GetMapping("/list")
    public Result getBookList(BookPageQuery query) {
        // 调用服务层分页查询
        PageInfo<Book> info = bookService.getBookPage(query);
        return Result.success(info);
    }

    /**
     * 根据id删除书籍
     */
    @AutoLog(value = "删除书籍")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        //开始执行删除功能
        log.info("开始执行删除功能");
        return Result.success(bookService.deleteById(id));
    }

    /**
     * 根据id查询书籍
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        return Result.success(bookService.findById(id));
    }

    /**
     * 获取图书类型统计--饼状图
     * @return
     */
    @GetMapping("/echarts/pie")
    public Result bie() {
        //查询所有图书
        List<Book> books = bookService.getAll();

        Map<String, Long> collect = books.stream()
                .filter(x -> ObjectUtil.isNotEmpty(x.getTypeName()))//过滤图书类型不为null的
                .collect(Collectors.groupingBy(Book::getTypeName, Collectors.counting()));//按图书类型分组

        //最后返回的
        List<Map<String, Object>> mapList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(collect)){
            for(String key:collect.keySet()){
                Map<String, Object> map = new HashMap<>();
                map.put("name", key);
                map.put("value", collect.get(key));
                mapList.add(map);
            }
        }

        return Result.success(mapList);
    }

    /**
     * 获取图书类型统计--柱状图
     * @return
     */
    @GetMapping("/echarts/bar")
    public Result bar() {
        //查询出所有图书
        List<Book> books = bookService.getAll();
        Map<String, Long> collect = books.stream()
                .filter(x -> ObjectUtil.isNotEmpty(x.getTypeName()))//过滤图书类型不为null的
                .collect(Collectors.groupingBy(Book::getTypeName, Collectors.counting()));

        List<String> xAxis = new ArrayList<>();
        List<Long> yAxis = new ArrayList<>();

        if (CollectionUtil.isNotEmpty(collect)){
            for(String key:collect.keySet()){
                xAxis.add(key);
                yAxis.add(collect.get(key));
            }
        }

        Map<String, Object> map = new HashMap<>();
        map.put("xAxis", xAxis);
        map.put("yAxis", yAxis);

        return Result.success(map);
    }
}
